package com.gd.iot.energy.controller.superadmin;

import com.gd.iot.energy.annotation.SystemLogRecordMonitor;
import com.gd.iot.energy.entity.ResponseEntity;
import com.gd.iot.energy.pojo.superadmin.dto.RoleSaveDTO;
import com.gd.iot.energy.pojo.common.vo.PageVO;
import com.gd.iot.energy.pojo.superadmin.vo.RolePageVO;
import com.gd.iot.energy.pojo.superadmin.vo.RoleVO;
import com.gd.iot.energy.service.superadmin.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 角色管理
 *
 * @author kris
 * @date Created at 2020/8/3 10:50
 */
@Api(value = "超级管理-用户管理-角色管理",tags = "超级管理-用户管理-角色管理")
@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("获取角色列表")
    @SystemLogRecordMonitor("获取角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", dataType = "string", paramType = "query", example = "", required = false),
    })
    @PostMapping("/getList")
    public ResponseEntity<List<RoleVO>> getList(@RequestParam(value = "name",required = false)String name) {

        List<RoleVO> roleVOs = roleService.getRoleVOs(name);
        return new ResponseEntity<>(roleVOs);
    }

    @ApiOperation("获取角色分页列表")
    @SystemLogRecordMonitor("获取角色分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", dataType = "string", paramType = "query", example = "", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "string", paramType = "query", example = "", required = false),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "string", paramType = "query", example = "", required = false),
            @ApiImplicitParam(name = "orderColumn", value = "排序字段", dataType = "string", paramType = "query", example = "", required = false),
            @ApiImplicitParam(name = "orderType", value = "排序类型(asc,desc)", dataType = "string", paramType = "query", example = "", required = false),
    })
    @PostMapping("/getPageList")
    public ResponseEntity<PageVO<RolePageVO>> getPageList(
            @RequestParam(value = "name",required = false)String name,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
            @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "orderColumn",required = false,defaultValue = "id") String orderColumn,
            @RequestParam(value = "orderType",required = false,defaultValue = "asc") String orderType
    ) {

        PageVO<RolePageVO> pageVO = roleService.getPageRoleVOs(name,pageSize,pageNum,orderColumn,orderType);
        return new ResponseEntity<>(pageVO);
    }

    @ApiOperation("获取角色")
    @SystemLogRecordMonitor("获取角色")
    @PostMapping("/get")
    public ResponseEntity<RoleSaveDTO> get(Integer id){
        RoleSaveDTO roleVO = roleService.getRole(id);
        return new ResponseEntity<>(roleVO);
    }

    @ApiOperation("新增角色")
    @SystemLogRecordMonitor("新增角色")
    @PostMapping("/add")
    public ResponseEntity<Integer> add(@RequestBody RoleSaveDTO roleSaveDTO){
        Integer id = roleService.add(roleSaveDTO);
        return new ResponseEntity<>(id);
    }

    @ApiOperation("更新角色")
    @SystemLogRecordMonitor("更新角色")
    @PostMapping("/update")
    public ResponseEntity<Boolean> update(@RequestBody RoleSaveDTO roleSaveDTO){
        boolean res = roleService.update(roleSaveDTO);
        return new ResponseEntity<>(res);
    }

    @ApiOperation("删除角色")
    @SystemLogRecordMonitor("删除角色")
    @PostMapping("/delete")
    public ResponseEntity<Boolean> delete(@RequestBody List<Integer> ids){
        boolean res = roleService.delete(ids);
        return new ResponseEntity<>(res);
    }
}
